package com.diudiutechnology.www.yadiudiu.util.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diudiutechnology.www.yadiudiu.R;

/**
 * Created by Administrator on 2017/8/15.
 */

public class ToastUtils {
    public static Toast toast = null;


    public static void show(Context context, int resId) {
        show(context, context.getResources().getText(resId), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration) {
        show(context, context.getResources().getText(resId), duration);
    }

    public static void show(Context context, CharSequence text) {
        show(context, text, Toast.LENGTH_SHORT,0);
    }

    public static void show(Context context, CharSequence text,int imgid) {
        show(context, text, Toast.LENGTH_SHORT,imgid);
    }

    private static ImageView imageCodeProject;
    private static LinearLayout.LayoutParams params;
    private static LinearLayout.LayoutParams imgParams;

    public static void show(Context context, CharSequence text, int duration, int imgid) {
        if (text == null || Sundry.isEmpty(text + "")) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(context, text, duration);
        }else {
            toast.setText(text);
        }

        //创建LinearLayout布局
        LinearLayout toastView = (LinearLayout) toast.getView();
        //设置LinearLayout的布局取向
        toastView.setOrientation(LinearLayout.HORIZONTAL);
        if (params == null) {
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            params.leftMargin = DisplayMetricsConverter.DipToPixel(context, 5);
        }
        toastView.setGravity(Gravity.CENTER);
        //文字View
        View view = toastView.getChildAt(toastView.getChildCount()-1);
        //给toastView添加View布局
        toastView.removeAllViews();
        if (imgid != 0) {
            if (imgParams == null) {
                int wh = DisplayMetricsConverter.DipToPixel(context, 15);
                imgParams = new LinearLayout.LayoutParams(wh, wh);
            }
            if (imageCodeProject == null) {
                //创建ImageView
                imageCodeProject = new ImageView(context);
            }
            imageCodeProject.setImageResource(imgid);
            toastView.addView(imageCodeProject,imgParams);
        }
        toastView.addView(view,params);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }




    public static void show(Context context, int resId, Object... args) {
        show(context,
                String.format(context.getResources().getString(resId), args),
                Toast.LENGTH_SHORT);
    }

    public static void show(Context context, String format, Object... args) {
        show(context, String.format(format, args), Toast.LENGTH_SHORT);
    }

    public static void show(Context context, int resId, int duration,
                            Object... args) {
        show(context,
                String.format(context.getResources().getString(resId), args),
                duration);
    }

    public static void show(Context context, String format, int duration,
                            Object... args) {
        show(context, String.format(format, args), duration);
    }
    /**
     * 显示Toast
     * @param context
     * @param tvString
     */

    public static void Toastshow (Context context,CharSequence tvString){
        if (tvString == null || Sundry.isEmpty(tvString + "")) {
            return;
        }
        View layout = LayoutInflater.from(context).inflate(R.layout.toast,null);
        TextView text = (TextView) layout.findViewById(R.id.text);
        Toast toasts=null;
        if (toasts == null) {
            toasts = Toast.makeText(context, tvString, Toast.LENGTH_SHORT);
        }
        text.setText(tvString);
        int screenWidth = Utils.getScreenWidth(context);
        int screenHeight = DisplayMetricsConverter.DipToPixel(context, 45);
        LinearLayout layout2 = (LinearLayout)layout.findViewById(R.id.toast_layout_root);
        layout2.getLayoutParams().width = screenWidth;
        layout2.getLayoutParams().height = screenHeight;
        toasts.setGravity(Gravity.TOP, 0, 0);
        toasts.setView(layout);
        toasts.setDuration(Toast.LENGTH_SHORT);
        toasts.show();

    }
}

